<?php
/**
 * User profile controller 
 * 
 * @author Konstantin Shamko <konstantin.shamko@gmail.com> 
 * @version 0.0.1
 * @copyright  Copyright (c) 2009 Konstantin Shamko
 * @category VaselinEngine
 * @package Usermanagement Module
 * @subpackage Controller
 * @license  New BSD License
 *
 */
class usermanagement_ProfileController extends Bel_Controller  {

	/**
	 * Logged in user data
	 *
	 * @var Zend_Db_Table_Row
	 */
	private $_auth;
	
	/**
	 * Initialization
	 *
	 */
	public function init(){
		parent::init ();
		$this->_auth = Models_Usermanagement_Auth::getInstance()->getUserSession();		
	}
	
	/**
	 * Displays profile form.
	 * IS NOT USED IN DEMO
	 *
	 */	
	public function indexAction(){
		$this->view->display('usermanagement/profile/profile_form.tpl');		
	}
	
	/**
	 * Displays tab with form to modify user general user info 
	 * (all except password)
	 *
	 */
	public function generaldataformAction(){
		$form = new Bel_Forms_Builder('registration_form','/usermanagement/profile/save');
		$form->removeElement('user_password')
			 ->removeElement('password2')
			 ->removeElement('captcha');
		
		$form->populateForm($this->_auth->toArray());
		
		/*if($this->_request->getParam('error')){
			$this->view->assign('messages',$this->_messages->getMessages());
		}*/
		
		$this->view->assign('form',$form);
		$this->view->display('forms/ajax.tpl');
	}
	
	/**
	 * Saves general user info after validation (username, email validations).
	 * If user changes an email afddress acount is switched into inactive state
	 * and email is sent to new email address with instructions on how to activate account
	 *
	 */
	public function saveAction(){
		$request = $this->_request->getParams();
		$oValidationHelper = new Helpers_Usermanagement_Validate();
		
		$error = false;
		if(!$oValidationHelper->ifEmailAvailable($request['user_email'],$this->_auth->user_id)){
			$error = true;
			$this->_messages->setMessage('User with such email already exists','error','user_email');
		}

		if(!$oValidationHelper->ifUsernameAvailable($request['user_login'],$this->_auth->user_id)){
			$this->_messages->setMessage('User with such username already exists','error','user_login');
			$error = true;
		}
				
		if($this->_validator->validate('registration_form',$request) && !$error){
			
			if($request['user_email']!=$this->_auth->user_email){
				$request['user_confirmation_code'] = md5(time()+rand(10000,99999));
				$request['user_active'] = 0;
				$this->_sendConfirmationEmail($request['user_email'],$request['user_login'],$request['user_confirmation_code']);
				$this->_messages->setMessage('WARNING!!! Your email has been changed and your account has been disabled. Instructions on how to activate account were sent to you','error');
			}
			
			$request['user_id'] = $this->_auth->user_id;
			$oUsers = new Models_Usermanagement_Users();
			$oUsers->updateEntry($request);			
			
			$this->_messages->setMessage('Account has been updated');
			$this->_redirect('/usermanagement/profile/generaldataform/');
		}else{
			foreach ( $this->_validator->getErrors () as $field=>$error ) {
				$this->_messages->setMessage ( $error, 'error' , $field);
			}
			$this->_redirect('/usermanagement/profile/generaldataform/?error=1');
		}
	}

	/**
	 * Displays form to change password
	 *
	 */
	public function passwordformAction(){
		/*if($this->_request->getParam('error')){
			$this->view->assign('messages',$this->_messages->getMessages());
		}*/
				
		$form = new Bel_Forms_Builder('passreset_form','/usermanagement/profile/savepassword');
		$this->view->assign('form',$form);
		$this->view->display('forms/ajax.tpl');		
	}	

	/**
	 * Saves new password after match validation
	 *
	 */
	public function savepasswordAction(){
		$request = $this->_request->getParams();
		
		$error = false;
		$oValidationHelper = new Helpers_Usermanagement_Validate();
		
		if(!$oValidationHelper->ifPasswordMatch($request['user_password'],$request['password2'])){
			$error = true;
			$this->_messages->setMessage('Passwords entered do not match','error','user_password');
		}	
		
		if($this->_validator->validate('passreset_form',$request) && !$error){
			$this->_auth->user_password = md5($request['user_password']);
			$this->_auth->save();
			$this->_messages->setMessage('Password has been updated');			
			$this->_redirect('/usermanagement/profile/passwordform');
		}else{
			foreach ( $this->_validator->getErrors () as $field=>$error ) {
				$this->_messages->setMessage ( $error, 'error' , $field);
			}
			$this->_redirect('/usermanagement/profile/passwordform/?error=1');
		}
	}
	
	/**
	 * Handles emailing. Sends account activation instructions
	 *
	 * @param string $email_to
	 * @param string $name_to
	 * @param string $confirmation_code
	 */
	private function _sendConfirmationEmail($email_to, $name_to, $confirmation_code){
		$mailer = new Bel_Mailer();
		$mailer->addTo ( $email_to );
		$mailer->setSubject('Reactivate your account');
		$this->view->assign('code',$confirmation_code);
		$this->view->assign('name',$name_to);
		$mailer->setBodyHtml ( $this->view->fetch('usermanagement/profile/confirmation_email.tpl') );
		$mailer->send ();
	}	
}